package cn.ekuma.epos.datalogic.define.dao;

import e.odbo.DB;
import e.odbo.data.dao.ModifiedLogDAO;
import cn.ekuma.epos.db.table.I_ProductAttributeValuePrice;

import com.openbravo.bean.Product;
import com.openbravo.bean.ProductAttributeValuePrice;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.serialize.DataParams;
import com.openbravo.data.loader.serialize.DataRead;
import com.openbravo.data.loader.serialize.DataWrite;
import com.openbravo.data.loader.serialize.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.serialize.serializer.read.SerializerReadDouble;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteParams;
import com.openbravo.data.loader.sentence.StaticSentence;
import com.openbravo.data.loader.table.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;
import com.openbravo.pos.bean.ProductExt;

public class ProductAttributeValuePriceDAO extends ModifiedLogDAO<ProductAttributeValuePrice>{

	public ProductAttributeValuePriceDAO(I_Session s) {
		super(s);
	}

	@Override
	public TableDefinition getTable() {
		return new TableDefinition(
                "PRODUCTS_ATTRIBUTEVALUE_PRICE",
                new Field[]{
                  new Field(I_ProductAttributeValuePrice.ID, Datas.STRING, Formats.STRING),
                  new Field(I_ProductAttributeValuePrice.PRODUCT_ID, Datas.STRING, Formats.STRING,Product.class),
                  new Field(I_ProductAttributeValuePrice.ATTNAME, Datas.STRING, Formats.STRING),
                  new Field(I_ProductAttributeValuePrice.DIFFPRICE, Datas.DOUBLE, Formats.DOUBLE),
                  new Field(I_ProductAttributeValuePrice.LASTMODIFIED,Datas.TIMESTAMP, Formats.TIMESTAMP),
                  new Field(I_ProductAttributeValuePrice.DIFFPRICECOST, Datas.DOUBLE, Formats.DOUBLE),
		},
        new int[]{0});
	}

	@Override
	public void writeInsertValues(DataWrite dp, ProductAttributeValuePrice obj)
			throws BasicException {
		dp.setString(1,obj.getId());
		dp.setString(2,obj.getProductId());
		dp.setString(3,obj.getAttname());
		dp.setDouble(4,obj.getDiffprice());
		dp.setTimestamp(5,obj.getLastModified());
		dp.setDouble(6,obj.getDiffPriceCost());
	}

	@Override
	public Class getSuportClass() {
		return ProductAttributeValuePrice.class;
	}

	@Override
	public ProductAttributeValuePrice readValues(DataRead dr,
			ProductAttributeValuePrice obj) throws BasicException {
		if(obj==null)
			obj=new ProductAttributeValuePrice();
		obj.setId(dr.getString(1));
		obj.setProductId(dr.getString(2));
		obj.setAttname(dr.getString(3));
		obj.setDiffprice(dr.getDouble(4));
		obj.setLastModified(dr.getTimestamp(5));
		obj.setDiffPriceCost(dr.getDouble(6));
		return obj;
	}
	
	public ProductAttributeValuePrice findProductAttributePrice(final String productId, final String value) throws BasicException{
		return  (ProductAttributeValuePrice) new StaticSentence(s,
				DB.getListSQL(this)+" WHERE PRODUCT_ID=? AND ATTNAME=?",
				SerializerWriteParams.INSTANCE, this)
				.find(new DataParams() {
					@Override
					public void writeValues() throws BasicException {
						setString(1, productId);
						setString(2, value);
					}
				});
	}

	@Override
    public Class transClass(Class in) {
        if(in==ProductExt.class)
            return Product.class;
        return super.transClass(in);
    }
}
